using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using SideShow.Scf.ObjectModel;

namespace PocketPCSideShowClient
{
    public class ContextMenuItemEventArgs : EventArgs
    {
        private ScfMenuItem _menuItem;

        public ContextMenuItemEventArgs(ScfMenuItem menuItem)
        {
            _menuItem = menuItem;
        }

        public ScfMenuItem MenuItem
        {
            get { return _menuItem; }
        }
    }

    public class ScfContextMenu
    {
        private ContextMenu _contextMenu;
        private ScfMenu _scfMenu;
        private ScfUserControl _ctrl;
        private ScfMenuItem _menuItem;

        public event EventHandler<ContextMenuItemEventArgs> ItemClick;
        public event EventHandler HomeClick;
        public event EventHandler BackClick;

        public ScfContextMenu(ScfMenu scfMenu, ScfMenuItem menuItem, ScfUserControl ctrl)
        {
            _scfMenu = scfMenu;
            _ctrl = ctrl;
            _menuItem = menuItem;
            CreateMenu();
        }

        private void CreateMenu()
        {
            _contextMenu = new ContextMenu();
            foreach (ScfObject scfItem in _scfMenu.Children)
            {
                MenuItem newItem = new MenuItem();
                if (scfItem is ScfMenuItem)
                {
                    ScfMenuItem scfMenuItem = (ScfMenuItem)scfItem;
                    newItem.Text = scfMenuItem.Text;
                    newItem.Enabled = scfMenuItem.Enabled;
                    newItem.Click += delegate(object sender, EventArgs args)
                    {
                        OnItemClick(scfMenuItem);
                    };
                }
                else if (scfItem is ScfDivider)
                {
                    newItem.Text = "-";
                }

                _contextMenu.MenuItems.Add(newItem);

            }
            // Add separator if there are already items added
            if (_contextMenu.MenuItems.Count > 0)
            {
                MenuItem newItem = new MenuItem();
                newItem.Text = "-";
                _contextMenu.MenuItems.Add(newItem);
            }
            // Add standard commands
            MenuItem sItem = new MenuItem();
            sItem.Text = "Home";
            sItem.Click += delegate(object sender, EventArgs args)
            {
                OnHomeClick();
            };
            _contextMenu.MenuItems.Add(sItem);
            sItem = new MenuItem();
            sItem.Text = "Back";
            sItem.Click += delegate(object sender, EventArgs args)
            {
                OnBackClick();
            };
            _contextMenu.MenuItems.Add(sItem);
        }

        protected void OnItemClick(ScfMenuItem ctxMenuItem)
        {
            _ctrl.Navigator.Navigate(_menuItem, _scfMenu, ctxMenuItem);
            if (ItemClick != null)
            {
                ItemClick(this, new ContextMenuItemEventArgs(ctxMenuItem));
            }
        }

        protected void OnBackClick()
        {
            _ctrl.Navigator.Back();
            if (BackClick != null)
            {
                BackClick(this, EventArgs.Empty);
            }
        }

        protected void OnHomeClick()
        {
            _ctrl.Navigator.Home();
            if (HomeClick != null)
            {
                HomeClick(this, EventArgs.Empty);
            }
        }

        public ContextMenu ContextMenu
        {
            get { return _contextMenu; }
        }
    }
}
